﻿using System;
using System.Collections.Generic;

using SWE4.Data.EmbeddedSensorCloud.DTO.Response;

namespace EmbeddedSensorCloud.Plugin.MicroERP
{
    public static class DictionaryStringExtension
    {
        public static string GetValueIfKey(this Dictionary<string, string> dictionary, string key)
        {
            if (dictionary.ContainsKey(key))
            {
                return dictionary[key];
            }
            else
            {
                return string.Empty;
            }
        }
    }

    public static class StringExtension
    {
        public static DateTime? ParseOrNullDateTime(this string text)
        {
            if(string.IsNullOrWhiteSpace(text))
            { return null; }

            DateTime tmp;
            return (DateTime.TryParse(text, out tmp)) ? (DateTime?)tmp : null;
        }

        public static decimal? ParseOrNullDecimal(this string text)
        {
            if(string.IsNullOrWhiteSpace(text))
            { return null; }

            decimal tmp;
            return (decimal.TryParse(text, out tmp)) ? (decimal?)tmp : null;
        }
    }

    public static class DTOResponseCustomerModelExtension
    {
        public static string GetDisplayName(this CustomerResponseModel customerModel)
        {
            if (customerModel is PersonResponseModel)
            {
                PersonResponseModel personModel = (PersonResponseModel)customerModel;
                return string.Format("{0} {1}", personModel.FirstName, personModel.LastName);
            }
            else
            {
                return (customerModel as CompanyResponseModel).Name;
            }
        }
    }
}
